package com.jushu.storbox.task.tqueue;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jushu.storbox.callback.ICallback;

public class DownloadCallbackWrapper implements ICallback {

	@Override
	public void onStart() {
		synchronized(callBackList) {
			Iterator<ICallback> it = callBackList.iterator();
			while(it.hasNext()) {
				ICallback callBack = it.next();
				callBack.onStart();
			}
		}
	}

	@Override
	public void onFailure(Throwable t, int statusCode) {
		synchronized(callBackList) {
			Iterator<ICallback> it = callBackList.iterator();
			while(it.hasNext()) {
				ICallback callBack = it.next();
				callBack.onFailure(t, statusCode);
			}
		}
	}

	@Override
	public void onSuccess() {
		synchronized(callBackList) {
			Iterator<ICallback> it = callBackList.iterator();
			while(it.hasNext()) {
				ICallback callBack = it.next();
				callBack.onSuccess();
			}
		}
	}

	@Override
	public void onProgress(int currentProgress, long currentSize, long maxSize) {
		synchronized(callBackList) {
			Iterator<ICallback> it = callBackList.iterator();
			while(it.hasNext()) {
				ICallback callBack = it.next();
				callBack.onProgress(currentProgress, currentSize, maxSize);
			}
		}
	}

	@Override
	public void onStop() {
		synchronized(callBackList) {
			Iterator<ICallback> it = callBackList.iterator();
			while(it.hasNext()) {
				ICallback callBack = it.next();
				callBack.onStop();
			}
		}
	}

	@Override
	public void onCanceled() {
		synchronized(callBackList) {
			Iterator<ICallback> it = callBackList.iterator();
			while(it.hasNext()) {
				ICallback callBack = it.next();
				callBack.onCanceled();
			}
		}
	}

	@Override
	public void onDettach() {
		synchronized(callBackList) {
			Iterator<ICallback> it = callBackList.iterator();
			while(it.hasNext()) {
				ICallback callBack = it.next();
				callBack.onDettach();
			}
		}
	}
	
	List<ICallback> callBackList = new ArrayList<ICallback>();

	public void registerCallback(ICallback callback) {
		synchronized(callBackList) {
			if(!callBackList.contains(callback)) {
				callBackList.add(callback);
			}
		}
	}
	
	public void unRegisterCallback(ICallback callback) {
		synchronized(callBackList) {
			if(callBackList.contains(callback)) {
				callBackList.remove(callback);
			}
		}
	}

}
